package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.RecordBean;
import com.project.bean.RoomBean;
import com.project.mapper.IRecordMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.ref.PhantomReference;
import java.util.List;

@Service
public class RecordService implements IRecordService {

    @Autowired
    private IRecordMapper recordMapper;
    @Autowired
    private IRoomMapper roomMapper;

    @Override
    public List<RecordBean> findByRoomId(Integer id) {
        QueryWrapper<RecordBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_roomID", id);
        return recordMapper.selectList(queryWrapper);
    }

    @Override
    public void addRecord(String record, Integer roomId) {
        recordMapper.insert(new RecordBean(record, roomId));
        RoomBean roomBean = roomMapper.selectById(roomId);
        roomBean.setStatus("损害");
        roomMapper.updateById(roomBean);
    }

    @Override
    public void updateRecord(Integer recordId, Integer roomId) {
        // 根据设施记录 ID 更新设施记录状态为已解决
        RecordBean recordBean = recordMapper.selectById(recordId); // 根据设施记录 ID 查询设施记录
        recordBean.setStatus("已解决"); // 将设施记录状态设置为已解决
        recordMapper.updateById(recordBean); // 更新设施记录状态

        // 查询房间中未解决的设施数量
        QueryWrapper<RecordBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_roomId", roomId); // 添加查询条件：设施记录的 fk_roomId 字段等于 roomId
        queryWrapper.eq("r_status", "未解决"); // 添加查询条件：设施记录的 r_status 字段等于 "未解决"
        List<RecordBean> recordList = recordMapper.selectList(queryWrapper); // 执行查询并将结果封装到 List 中
        if (recordList.size() == 0) { // 如果未解决设施数量为 0
            RoomBean roomBean = roomMapper.selectById(roomId); // 根据房间 ID 查询房间信息
            roomBean.setStatus("正常"); // 将房间状态设置为正常
            roomMapper.updateById(roomBean); // 更新房间状态
        }

    }
}
